"""
====================================================================
Copyright (C) 2012-2013 Eric Blond

This file is part of Neptune.

Neptune is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

Neptune is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with Neptune.  If not, see <http://www.gnu.org/licenses/>.
====================================================================

Created on 27 Jun 2012

@author: Eric
"""

import logging
import neptune.nucleus.public as nucleus
import neptune.spark.public as spark

#######

LOGGER = logging.getLogger(__name__)

#######

class App(nucleus.Page):

    @nucleus.cell
    def Menu3(self):
        return spark.MenuItem(Text = 'Menu3')

    def on_menu2_click(self):
        LOGGER.info('Menu2 clicked!')
        self.Menu3.IsEnabled = not self.Menu3.IsEnabled

    @nucleus.cell
    def FileMenu(self):
        return spark.Menu(Items = [spark.MenuItem(Text    = 'Menu1',
                                                  OnClick = self.on_menu2_click),
                                   spark.MenuItem(Text = 'Menu2'),
                                   None,
                                   self.Menu3,
                                   None,
                                   self.SelectedMenuItem])

    @nucleus.cell
    def SelectedMenuItem(self):
        return spark.CheckMenuItem(Text = 'Selected ?',
                                   OnClick = self.on_set_menu_click)

    def on_set_menu_click(self):
        LOGGER.info('Selected menu clicked? %s', self.SelectedMenuItem.IsChecked)

    @nucleus.cell
    def MenuBar(self):
        return spark.MenuBar(Menus = [('File', self.FileMenu),
                                      ('Edit', spark.Menu())])

    @nucleus.cell
    def Child(self):
        return spark.GridLayout(Children = [[spark.Button(Label = 'One'),
                                             spark.Button(Label = 'Two')],
                                            spark.Button(Label = 'Three')])

    @nucleus.cell
    def Window(self):
        return spark.Window(Title = 'Test!',
                            MenuBar = self.MenuBar,
                            OnClose = self.on_close,
                            Child = self.Child)

    @nucleus.cell
    def Gui(self):
        return spark.Gui(Windows = [self.Window],
                         OnStart = self.on_start,
                         OnStop = self.on_stop)

    def on_close(self):
        LOGGER.info('Closed!')

    def on_start(self):
        LOGGER.info('Started!')
    
    def change_title(self):
        self.Frame.Title = 'Test 2!'

    def on_stop(self):
        LOGGER.info('Stopped!')

#######

if __name__ == '__main__':
    logging.basicConfig()
    logging.getLogger().setLevel(logging.INFO)
    nucleus.config.LOG_CELLFN_EXCEPTIONS = True
    spark.run(App)
